package cz.via.wowss.dao;

import java.util.List;

import javassist.NotFoundException;

import javax.persistence.EntityManager;

import cz.via.wowss.controller.MainController;
import cz.via.wowss.entities.Company;
import cz.via.wowss.entities.CompanyHistory;
import cz.via.wowss.entities.News;
import cz.via.wowss.entities.Shareholder;

public class CompanyDao implements ICompanyDao {

	public Company getCompany(int id) {
		EntityManager em = MainController.emf.createEntityManager();
		Company entity = null;
		try {
			entity = em.find(Company.class, id);
			if (entity == null)
				try {
					throw new NotFoundException(
							"Could not find any company with ID " + id);
				} catch (NotFoundException e) {
					e.printStackTrace();
				}
		} finally {
			em.close();
		}
		return entity;
	}

	public Company getCompany(String name) {
		EntityManager em = MainController.emf.createEntityManager();
		Company entity = null;
		try {
			entity = em.createQuery(
					"SELECT c FROM Company c WHERE c.name='" + name + "'",
					Company.class).getSingleResult();
			if (entity == null)
				try {
					throw new NotFoundException(
							"Could not find any company with name " + name);
				} catch (NotFoundException e) {
					e.printStackTrace();
				}
		} finally {
			em.close();
		}
		return entity;
	}

	public List<Company> getCompanies() {
		return MainController.emf.createEntityManager()
				.createQuery("SELECT c FROM Company c", Company.class)
				.getResultList();
	}

	// Zatim se nepouziva order, filter, maxResults, offset
	public List<Company> getCompanies(String order, String filter,
			int maxResults, int offset) {
		return MainController.emf.createEntityManager()
				.createQuery("SELECT c FROM Company c", Company.class)
				.getResultList();
	}

	public int getCompanyId(String name) {
		return getCompany(name).getId();
	}

	public List<News> getNews(int companyId, int type, int offset) {
		return MainController.emf
				.createEntityManager()
				.createQuery(
						"SELECT n FROM News n WHERE n.company=" + companyId
								+ " OR n.companyType=" + type
								+ " ORDER BY n.date DESC", News.class)
				.setFirstResult(offset).setMaxResults(10).getResultList();
	}

	public List<News> getNewsByType(int offset, int type) {
		return MainController.emf
				.createEntityManager()
				.createQuery(
						"SELECT n FROM News n WHERE n.companyType=" + type
								+ " ORDER BY n.date DESC", News.class)
				.setFirstResult(offset).setMaxResults(10).getResultList();
	}

	public List<News> getNews(int offset) {
		return MainController.emf
				.createEntityManager()
				.createQuery("SELECT n FROM News n ORDER BY n.date DESC",
						News.class).setFirstResult(offset).setMaxResults(10)
				.getResultList();
	}

	public List<Shareholder> getShares(int playerId) {
		return MainController.emf
				.createEntityManager()
				.createQuery(
						"SELECT s FROM Shareholder s WHERE s.player.id="
								+ playerId, Shareholder.class).getResultList();
	}

	public List<CompanyHistory> getHistory(int companyId, String from, String to) {
		return MainController.emf
				.createEntityManager()
				.createQuery(
						"SELECT h FROM CompanyHistory h WHERE h.company.id = "
								+ companyId + " AND h.date >= '" + from
								+ "' AND h.date <= '" + to + "'",
						CompanyHistory.class).getResultList();
	}

}
